package com.longcai.service.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.PageHelper;
import com.longcai.entity.sys.SysLog;
import com.longcai.mapper.SysLogMapper;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.sys.SysLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ZJ
 * @since 2019-03-16
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private SysLogMapper sysLogMapper;

    //保存日志
    @Override
    public Integer save(SysLog sysLog) throws Exception {

        Integer integer = this.sysLogMapper.insert(sysLog);

        return integer;
    }

    //根据UserId查询日志
    @Override
    public SysLog findById(String userId) throws Exception {

        return sysLogMapper.selectById(userId);
    }

    // 分页查询日志
    @Override
    public PageTableResponse sysLogListPage(SysLog sysLog, PageHelper pageHelper) {

        Page<SysLog> page = new Page<SysLog>(pageHelper.getStart(), pageHelper.getLength());

        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(sysLog.getUserId())) {
            queryWrapper.eq("user_id", sysLog.getUserId());
        }
        if (!StringUtils.isEmpty(sysLog.getUsername())) {
            queryWrapper.eq("username", sysLog.getUsername());
        }
        if (!StringUtils.isEmpty(sysLog.getIp())) {
            queryWrapper.eq("ip", sysLog.getIp());
        }
        if (!StringUtils.isEmpty(sysLog.getCTime())) {
            queryWrapper.eq("c_time", sysLog.getCTime());
        }

        IPage<SysLog> sysLogIPage = this.sysLogMapper.selectPage(page, queryWrapper);
        PageTableResponse pageTableResponse = new PageTableResponse(sysLogIPage.getTotal(), sysLogIPage.getTotal(), sysLogIPage.getRecords());
        return pageTableResponse;


    }

    //     删除日志
    @Override
    public void delSysLog(String userId) {
        try {
            this.sysLogMapper.deleteById(userId);
            logger.debug("删除成功+++++++++", userId);
        } catch (Exception e) {
            logger.error("删除失败+++++++++++", userId);

        }

    }

}
